package com.gitee.qdbp.base.shiro.realm;

import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import com.gitee.qdbp.base.system.model.IAccount;

/**
 * 可控的登录处理逻辑
 *
 * @author zhaohuihua
 * @version 180830
 */
public abstract class ManagableLoginRealm extends AuthorizingRealm {

    protected String getPrincipalCacheKey(PrincipalCollection principals) {
        Object principal = principals.getPrimaryPrincipal();
        if (principal instanceof IAccount) {
            return ((IAccount) principal).getId();
        } else {
            return null;
        }
    }

    /** 公开此方法提供给外部调用 **/
    public Object getAuthorizationCacheKey(PrincipalCollection principals) {
        Object principal = getPrincipalCacheKey(principals);
        return principal != null ? principal : principals;
    }

    /** 公开此方法提供给外部调用 **/
    public Object getAuthenticationCacheKey(PrincipalCollection principals) {
        Object principal = getPrincipalCacheKey(principals);
        return principal != null ? principal : principals;
    }

    /** 公开此方法提供给外部调用 **/
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    /** 公开此方法提供给外部调用 **/
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

}
